<?php
/**
* @package RokIntroScroller
* @copyright Copyright (C) 2007 RocketWerx. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

require_once (JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');
jimport('joomla.utilities.date');

$vmparser = JPATH_SITE.DS.'components'.DS.'com_virtuemart'.DS.'virtuemart_parser.php';
if (file_exists($vmparser)) require_once($vmparser);

class modRokVirtuemartScrollerHelper
{
		
	function getProducts(&$params)
	{
		global $mainframe, $my, $mosConfig_offset;

		$user		=& JFactory::getUser();
		$dispatcher	=& JDispatcher::getInstance();
		$userId		= (int) $user->get('id');

		$count		= (int) $params->get('count', 5);
		$aid		= $user->get('aid', 0);
		$show_title = trim($params->get( 'show_title', 1 ) );
		$show_price = trim($params->get( 'show_price', 0 ) );
		$sorting    = $params->get('sorting', 'random');
		$featured   = $params->get('featured', 'no');
		
		$contentConfig = &JComponentHelper::getParams( 'com_content' );
		$access		= !$contentConfig->get('shownoauth');

		$database = new ps_DB();
		require_once(CLASSPATH."ps_product.php");
		$ps_product = new ps_product;

		if($count>0) {
			$count = "LIMIT $count";
		} else {
			$count = "";
		}

		$query = "SELECT p.product_sku FROM #__{vm}_product AS p";

		$query .= "\nJOIN #__{vm}_product_category_xref as pc ON p.product_id=pc.product_id";
		/*if( $category_id != 0 ) {
			$query .= " AND pc.category_id=$category_id";
		}*/
		$query .= "\nJOIN #__{vm}_category as c ON pc.category_id=c.category_id";
		
		$query .= "\n WHERE p.product_publish = 'Y' AND c.category_publish = 'Y' AND product_parent_id=0 ";
		if( CHECK_STOCK && PSHOP_SHOW_OUT_OF_STOCK_PRODUCTS != "1") {
			$query .= " AND product_in_stock > 0 ";
		}
		
		if( $featured=='yes' ) {
			$query .= "\n AND product_special = 'Y' ";
		}
		
		switch( $sorting ) {
			case 'random':
				$query .= "\n ORDER BY RAND() $count";
				break;
			case 'newest':
				$query .= "\n ORDER BY p.cdate DESC $count";
				break;
			case 'oldest':
				$query .= "\n ORDER BY p.cdate ASC $count";
				break;
			default:
				$query .= "\n ORDER BY p.cdate DESC $count";
				break;
		}
		$database->query( $query );

		$rows = $database->record;

		// Process the prepare content plugins
		JPluginHelper::importPlugin('content');

		$i		= 0;
		$lists	= array();
		foreach ( $rows as $row )
		{
			$lists[$i] = $ps_product->product_snapshot($row->product_sku, $show_price, true, $show_title);
			$i++;
		}

		return $lists;
	}

	function _getJSVersion() {
		if (version_compare(JVERSION, '1.5', '>=') && version_compare(JVERSION, '1.6', '<')){
			if (JPluginHelper::isEnabled('system', 'mtupgrade')){
				return "-mt1.2";
			} else {
				return "";
			}
		} else {
			return "";
		}
	}

}
